import Vue from "vue";
import VueRouter from "vue-router";
import { getToken } from "../utils/token";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: () => import("../views/login/index.vue")
  },
  {
    path: "/index",
    component: () => import("../layout/index"),
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        component: () => import("../views/welcome/index.vue")
      },
      { path: "/users", component: () => import("../views/users/index.vue") },
      { path: "/roles", component: () => import("../views/roles/index.vue") },
      { path: "/rights", component: () => import("../views/rights/index.vue") },
      {
        path: "/order",
        component: () => import("../views/order/index.vue")
      },
      {
        path: "/reports",
        component: () => import("../views/reports/index.vue")
      }
    ]
  }
];

const router = new VueRouter({
  routes
});

router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  // const tokenStr = window.sessionStorage.getItem("token");
  const tokenStr = getToken();
  if (!tokenStr) return next("/login");
  next();
});

export default router;
